package com.Humanities.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.model.RestResponse;
import com.Humanities.ucenter.mapper.XcUserMapper;
import com.Humanities.ucenter.model.dto.FindPasswordDTO;
import com.Humanities.ucenter.model.dto.RegisterDTO;
import com.Humanities.ucenter.model.po.XcUser;
import com.Humanities.ucenter.service.AccountPasswordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class XcuserController {
    @Autowired
    XcUserMapper xcUserMapper;
    @Autowired
    AccountPasswordService accountPasswordService;
    /**
     * 通过邮箱或者手机号查询用户
     *  自定义feign接口
     */
    @GetMapping("/id")
    public XcUser findXcuserByPhone(@RequestParam("s1") String s1) {
        //如果s1是手机号，包含11位数字
        if (s1.matches("^[0-9]{11}$")) {
            //手机号
            return xcUserMapper.selectOne(new LambdaQueryWrapper<XcUser>().eq(XcUser::getCellphone, s1));
        } else {
            //邮箱
            return xcUserMapper.selectOne(new LambdaQueryWrapper<XcUser>().eq(XcUser::getEmail, s1));
        }
    }

    /**
     * 找回密码/忘记密码
     *
     */
     @PostMapping("/findpassword")
    public RestResponse findPassword(@RequestBody @Validated FindPasswordDTO findPasswordDTO) {
       return accountPasswordService.findPassword(findPasswordDTO);
    }
    /**
     * 注册账号
     */
    @PostMapping("/register")
    public RestResponse register(@RequestBody @Validated RegisterDTO registerDTO){
        return accountPasswordService.register(registerDTO);
    }




}
